'''
@author: ShiHui
'''

# coding: utf-8
#!/usr/bin/python

import socket
import sys
import re

HOST = '127.0.0.1'    # The remote host
PORT = 11211              # The same port as used by the server
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except socket.error as msg:
        s = None
        continue
    try:
        s.connect(sa)
    except socket.error as msg:
        s.close()
        s = None
        continue
    break
if s is None:
    print 'could not open socket'
    sys.exit(1)
s.sendall('stats\r\n')
data = s.recv(2048)
s.close()
#print 'Received', repr(data)

word = re.split(ur"STAT (.*) (.*)\r", data)
#print word

_stat_regex = re.compile(ur"STAT (.*) (.*)\r")
_stats = dict(_stat_regex.findall(data))
print _stats

